package com.yuxi.controller.front;

import com.yuxi.common.FileConfig;
import com.yuxi.common.SkList;
import com.yuxi.common.StringUtil;
import com.yuxi.common.constant.PageKeyEnum;
import com.yuxi.common.html.SkContextModel;
import com.yuxi.enums.BannerPositionFlagEnum;
import com.yuxi.enums.ProductTypeTypeFlagEnum;
import com.yuxi.modelExt.BannerExt;
import com.yuxi.modelExt.ProductExt;
import com.yuxi.modelExt.ProductTypeExt;
import com.yuxi.pubInter.BaseController;
import com.yuxi.pubInter.baseVO.DecoratePageList;
import com.yuxi.pubInter.exception.SkException;
import com.yuxi.searchExt.ProductSearchExt;
import com.yuxi.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 共用数据
 *
 * @author sunke
 * @version 1.0
 * @date 2021-08-06 09:39
 */
public abstract class ConfigController extends BaseController {


    @Resource
    private IProductService productService;
    @Resource
    private IPartnerService partnerService;

    @Resource
    private IProductTypeService productTypeService;
    @Resource
    private IBannerService bannerService;

    @Autowired
    private FileConfig fileConfig;
    @Resource
    private IHonorService honorService;

    /**
     * 生成文件的目录
     *
     * @return
     */
    public abstract String getSaveHtmlDir();

    public abstract String getLang();

    public abstract String getViewRoute();

/*    @RequestMapping({"search.html"})
    public String search(@RequestParam(required = false) String keyword, SkContextModel skModel, Model model, HttpServletRequest request) throws SkException {

        skModel.setTemplateName(getViewRoute() + "search");
        skModel.setHtmlName(getSaveHtmlDir() + "search");
        skModel.setCreateHtmlFlag(false);


        List<ProductExt> productList = productService.getListBySizeAndKeywordAndLang(-1, keyword, getLang());

        int productSize = 0;
        fullBannerList(BannerPositionFlagEnum.EN_PC_INDEX.getType().intValue(), -1, skModel, getLang());

        skModel.addAttribute("productList", productList);
        if (StringUtil.isNotObjEmpty(productList)) {
            productSize = productList.size();
        }
        skModel.addAttribute("productSize", productSize);


        skModel.addAttribute("searchWordList", searchKeywordService.getListBySizeAndLang(-1, getLang()));

        skModel.addAttribute("certificateList", certificateService.getListBySizeAndLang(-1, getLang()));


        return returnView("search", skModel, request);
    }*/

    public abstract String getRoute();

    @RequestMapping({"index.html", "/"})
    public String index(SkContextModel skModel, Model model, HttpServletRequest request) throws SkException {
        skModel.setTemplateName(getViewRoute() + "index");
        skModel.setHtmlName(getSaveHtmlDir() + "index");


        fullBannerList(BannerPositionFlagEnum.INDEX_TOP.getType().intValue(), 5, skModel);

        fullBannerList("aboutBannerList", BannerPositionFlagEnum.INDEX_ABOUT.getType().intValue(), 5, skModel);
        fullBannerList("bottomBannerList", BannerPositionFlagEnum.INDEX_BOTTOM.getType().intValue(), 1, skModel);
        skModel.addAttribute("productList", productService.getListBySizeAndLang(8, getLang()));
        skModel.addAttribute("partnerList", partnerService.getListBySizeAndLang(-1, getLang()));
        skModel.addAttribute("newProduct", productService.getNew());
        skModel.addAttribute("recommendProductList", productService.getRecommendList(8));

        return returnView("index", skModel, request);
    }

    @RequestMapping({"self.html", "product_list/{upId}.html", "product_list/{upId}/{typeId}.html", "product_list/{pageNumber:[0-9]+}.html", "product_list/{upId}/{pageNumber:[0-9]+}.html", "product_list/{upId}/{typeId}/{pageNumber:[0-9]+}.html"})
    public String product(@PathVariable(required = false) Integer pageNumber, @PathVariable(required = false) String upId, @PathVariable(required = false) String typeId,

                          SkContextModel skModel, Model model, HttpServletRequest request) throws SkException {
        skModel.setTemplateName(getViewRoute() + "self");
        // skModel.setHtmlName(getSaveHtmlDir() + "self");
        if (StringUtil.isNotObjEmpty(upId)) {
            skModel.addHtml(getSaveHtmlDir() + "product_list/" + upId);
            if (StringUtil.isNotObjEmpty(typeId)) {
                skModel.addHtml(getSaveHtmlDir() + "product_list/" + upId + "/" + typeId);
                if (pageNumber != null) {
                    skModel.addHtml(getSaveHtmlDir() + "product_list/" + upId + "/" + typeId + "/" + pageNumber);
                }
            }
            if (pageNumber != null) {
                skModel.addHtml(getSaveHtmlDir() + "product_list/" + upId + "/" + pageNumber);
            }

        } else {
            skModel.setHtmlName(getSaveHtmlDir() + "self");
        }
        if (pageNumber != null) {
            skModel.addHtml(getSaveHtmlDir() + "product_list/" + pageNumber);
        }

        String keyword = request.getParameter("keyword");


        fullBannerList(BannerPositionFlagEnum.PRODUCT_LIST.getType().intValue(), 1, skModel);
        List<ProductTypeExt> typeList = productTypeService.getSubProductTypeList(-1, upId);
        skModel.addAttribute("productSubTypeList", typeList);

        ProductSearchExt productSearch = ProductSearchExt.getInstance();
        System.out.println(keyword);
        if (StringUtil.isNotObjEmpty(keyword)) {
            System.out.println("搜索内容未：" + keyword);
            productSearch.setNameLike(keyword);
        }

        if (pageNumber == null) {
            pageNumber = 1;
        }

        List<String> typeIds = SkList.getInstance();


        if (StringUtil.isNotObjEmpty(typeId)) {
            typeIds.add(typeId);
        }

        //说明只传了顶级
        if (StringUtil.isNotObjEmpty(upId) && StringUtil.isNullOrEmpty(typeId)) {
            //当前一级也加进去
            typeIds.add(upId);

            //添加所有下级
            if (StringUtil.isNotObjEmpty(typeList)) {
                typeList.stream().forEach(item -> {
                    typeIds.add(item.getId());
                });

            }


        }
        if (StringUtil.isNotObjEmpty(typeIds)) {
            productSearch.setTypeIds(typeIds);
        }


      /*  if (StringUtil.isNotObjEmpty(upId) && StringUtil.isObjEmpty(typeId)) {
            productSearch.setProductTypeIdEq(upId);
            productSearch.setUpIdEq(upId);
        }*/


        productSearch.setPageNumber(pageNumber);
        productSearch.setTypeFlagIn(SkList.getInstance().addObjToList(ProductTypeTypeFlagEnum.PRODUCT_TYPE.getType()));
        productSearch.setPageSize(20);
//        productSearch.setOrderBy("p_sort_num");


        DecoratePageList<ProductExt> productPage = productService.paginateToObject(productSearch);


        skModel.addAttribute("productList", productPage.getList());
        skModel.addAttribute("page", productPage.getPage());
        skModel.addAttribute("upId", upId);
        skModel.addAttribute("typeId", typeId);
        skModel.addAttribute("keyword", keyword);

        if (StringUtil.isNotObjEmpty(upId)) {
            skModel.addAttribute("allList", "/product_list/" + upId + ".html");
        }


        return returnView("self", skModel, request);
    }

    @RequestMapping({"product_detail_{id}.html"})

    public String productDetail(

            @PathVariable(required = false) String id,

            SkContextModel skModel, Model model, HttpServletRequest request) throws SkException {


        skModel.setTemplateName(getViewRoute() + "detail");
        skModel.setHtmlName(getSaveHtmlDir() + "product_detail_" + id);

        skModel.addAttribute("domain", productService.getDetail(id));
        fullBannerList(BannerPositionFlagEnum.PRODUCT_DETAIL.getType().intValue(), 1, skModel);

        return returnView("detail", skModel, request);


    }

    @RequestMapping({"agency.html", "agency_list/{upId}.html", "agency_list/{upId}/{typeId}.html", "agency_list/{pageNumber:[0-9]+}.html", "agency_list/{upId}/{pageNumber:[0-9]+}.html", "agency_list/{upId}/{typeId}/{pageNumber:[0-9]+}.html"

    })
    public String agency(@PathVariable(required = false) Integer pageNumber, @PathVariable(required = false) String upId,
                         @PathVariable(required = false) String typeId, SkContextModel skModel, Model model, HttpServletRequest request) throws SkException {
        skModel.setTemplateName(getViewRoute() + "agency");

        if (StringUtil.isNotObjEmpty(upId)) {
            skModel.addHtml(getSaveHtmlDir() + "agency_list/" + upId);
            if (StringUtil.isNotObjEmpty(typeId)) {
                skModel.addHtml(getSaveHtmlDir() + "agency_list/" + upId + "/" + typeId);
                if (pageNumber != null) {
                    skModel.addHtml(getSaveHtmlDir() + "agency_list/" + upId + "/" + typeId + "/" + pageNumber);
                }
            }
            if (pageNumber != null) {
                skModel.addHtml(getSaveHtmlDir() + "agency_list/" + upId + "/" + pageNumber);
            }

        } else {
            skModel.setHtmlName(getSaveHtmlDir() + "agency");
        }
        if (pageNumber != null) {
            skModel.addHtml(getSaveHtmlDir() + "agency_list/" + pageNumber);
        }

        List<ProductTypeExt> typeList = productTypeService.getSubBrandTypeList(-1, upId);

        fullBannerList(BannerPositionFlagEnum.BRAND_LIST.getType().intValue(), 1, skModel);
        skModel.addAttribute("productSubTypeList", typeList);

        ProductSearchExt productSearch = ProductSearchExt.getInstance();
        if (pageNumber == null) {
            pageNumber = 1;
        }

        List<String> typeIds = SkList.getInstance();
        if (StringUtil.isNotObjEmpty(typeId)) {
            typeIds.add(typeId);
        }
        //说明只传了顶级
        if (StringUtil.isNotObjEmpty(upId) && StringUtil.isNullOrEmpty(typeId)) {
            //当前一级也加进去
            typeIds.add(upId);

            //添加所有下级
            if (StringUtil.isNotObjEmpty(typeList)) {
                typeList.stream().forEach(item -> {
                    typeIds.add(item.getId());
                });

            }


        }
        if (StringUtil.isNotObjEmpty(typeIds)) {
            productSearch.setTypeIds(typeIds);
        }


        productSearch.setPageNumber(pageNumber);
        productSearch.setTypeFlagIn(SkList.getInstance().addObjToList(ProductTypeTypeFlagEnum.BRAND.getType()));
        productSearch.setPageSize(20);
        DecoratePageList<ProductExt> productPage = productService.paginateToObject(productSearch);


        skModel.addAttribute("productList", productPage.getList());
        skModel.addAttribute("page", productPage.getPage());
        skModel.addAttribute("upId", upId);
        skModel.addAttribute("typeId", typeId);

        if (StringUtil.isNotObjEmpty(upId)) {
            skModel.addAttribute("allList", "/agency_list/" + upId + ".html");
        }


        return returnView("agency", skModel, request);
    }

    @RequestMapping({"agency_detail_{id}.html"})
    public String agencyDetail(@PathVariable(required = false) String id, SkContextModel skModel, Model model, HttpServletRequest request) throws SkException {
        skModel.setTemplateName(getViewRoute() + "detail");
        skModel.setHtmlName(getSaveHtmlDir() + "agency_detail_" + id);
        skModel.addAttribute("domain", productService.getDetail(id));
        fullBannerList(BannerPositionFlagEnum.BRAND_DETAIL.getType().intValue(), 1, skModel);
        return returnView("agency_detail", skModel, request);
    }

    @RequestMapping({"about.html"})
    public String about(SkContextModel skModel, Model model, HttpServletRequest request) throws SkException {
        skModel.setTemplateName(getViewRoute() + "about");
        skModel.setHtmlName(getSaveHtmlDir() + "about");
        skModel.addAttribute("honorList", honorService.getListBySizeAndLang(-1, null));
        fullBannerList(BannerPositionFlagEnum.ABOUT.getType().intValue(), 1, skModel);
        fullBannerList("advantageBannerList", BannerPositionFlagEnum.ADVANTAGE.getType().intValue(), 4, skModel);

        return returnView("about", skModel, request);
    }


    @RequestMapping({"contact.html"})
    public String contact(SkContextModel skModel, Model model, HttpServletRequest request) throws SkException {
        skModel.setTemplateName(getViewRoute() + "contact");
        skModel.setHtmlName(getSaveHtmlDir() + "contact");
        skModel.addAttribute("honorList", honorService.getListBySizeAndLang(-1, null));
        fullBannerList(BannerPositionFlagEnum.LINK_US.getType().intValue(), 1, skModel);

        return returnView("contact", skModel, request);
    }

    public void fullBannerList(String keyName, int position, int num, SkContextModel model) {

        List<BannerExt> bannerList = bannerService.getListByPositionFlagAndSize(position, num);

        model.addAttribute(keyName, bannerList);


    }

    public void fullBannerList(int position, int num, SkContextModel model) {

        fullBannerList("bannerList", position, num, model);


    }


    public String returnView(String viewName, SkContextModel model, HttpServletRequest request) {

        model.addAttribute("route", getRoute());
        model.addAttribute("routeView", getViewRoute());
        model.setLang(getLang());
        model.addAttribute("lang", getLang());
        model.addAttribute("keyMap", PageKeyEnum.getPageMapByLang(getLang()));
        model.addAttribute("productTypeList", productTypeService.getTopProductTypeList(-1));
        model.addAttribute("brandTypeList", productTypeService.getTopBrandTypeList(-1));


        return getViewRoute() + viewName;


    }


}
